﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToggleGroupEx : ToggleGroup
{
    private Action<int, string> callBack;
    private Action<int, string> disAbleCallBack;
    private List<ToggleEx> ListItem = new List<ToggleEx>();
    private int indexToggled = -1;
    public void Init(Action<int, string> handler, Action<int, string> disableHandler = null)
    {
        indexToggled = -1;
        callBack = handler;
        disAbleCallBack = disableHandler;
        ListItem.Clear();
        foreach (Transform iter in transform)
        {
            ToggleEx item = iter.gameObject.GetComponent<ToggleEx>();
            if (!item)
                continue;

            ListItem.Add(item);
            item.Init(ListItem.Count - 1, OnSelecteChanged, OnUnSelecteChanged);
        }

        foreach (var item in ListItem)
            item.gameObject.GetComponent<Toggle>().isOn = false;
    }

    public void ClearItemList()
    {
        indexToggled = -1;
        ListItem.Clear();
    }

    public void AddItem(GameObject go)
    {
        ToggleEx temp = go.GetComponent<ToggleEx>();
        if (!temp)
            temp = go.AddComponent<ToggleEx>();
        temp._Toggle.isOn = false;
        ListItem.Add(temp);
        temp.Init(ListItem.Count - 1, OnSelecteChanged, OnUnSelecteChanged);
    }

    public void OnSelecteChanged(int index, string name)
    {
        indexToggled = index;

        if (callBack != null)
            callBack(index, name);
    }

    public void OnUnSelecteChanged(int index, string name)
    {
        if (index == indexToggled)
        {
            indexToggled = -1;
        }

        if (disAbleCallBack != null)
            disAbleCallBack(index, name);
    }

    /// <summary>
    /// 打开Toggle
    /// </summary>
    /// <param name="index">下标从0开始</param>
    public void TurnOnToggle(int index)
    {
        if (ListItem.Count > index && 0 <= index)
        {
            for (int i = 0; i < ListItem.Count; ++i)
            {
                if (ListItem[i] != null)
                    ListItem[i]._Toggle.isOn = (i == index);
            }
        }
        else
        {
            SetAllTogglesOff();
        }
    }

    public GameObject GetToggleByIndex(int index)
    {
        if (ListItem.Count > index && 0 <= index)
        {
            if (ListItem[index] != null)
            {
                // Debug.LogError(ListItem[index].GetComponent<RectTransform>().anchoredPosition);
                return ListItem[index].gameObject;
            }
        }

        return null;
    }

    public int GetToggleOnCount()
    {
        int count = 0;
        for (int i = 0; i < ListItem.Count; ++i)
        {
            if (ListItem[i]._Toggle.isOn)
            {
                ++count;
            }
        }

        return count;
    }

    protected override void OnEnable()
    {
        StartCoroutine(UpdateAnimState());
    }

    IEnumerator UpdateAnimState()
    {
        yield return new WaitForEndOfFrame();

        if (enabled)
        {
            for (int i = 0; i < ListItem.Count; ++i)
            {
                if (ListItem[i] != null)
                    ListItem[i].UpdateAnimState(indexToggled == i);
            }
        }
    }
}